# !/usr/bin/python
# -*- coding: utf-8 -*-

# THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),  
# to deal in the Software without restriction, including without limitation  
# the rights to use, copy, modify, merge, publish, distribute, sublicense,  
# and/or sell copies of the Software, and to permit persons to whom the  
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in 
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS IN THE SOFTWARE.

# menu.py
# written by: 
#     Jan Jokela <jan@devion.org>

import os, sys, time
from qgl import scene
from pygame.locals import *
from fluid.eye import *
import fluid.actions as actions
import fluid.visuals as visuals
import topbar
import background
import base.config.config as config
import base.config.data as config_data


class MainMenu(object):
    def __init__(self):
        eventhandler.subscribe(self)
        self.topbar = topbar.TopBar(self)
        self.background = background.Background()
        self.pluginlist = PluginList(self)

    def handle_events(self, events):
        if config.input == "keyboard + mouse":
            for event in events:
                if event.type is KEYDOWN:
                    if event.key == K_RETURN:
                        self.destroy()
                        import plugins.music.menu as menu
                        menu.MusicMenu()
                        root_node.accept(compiler)   
                    
    def destroy(self):
        eventhandler.unsubscribe(self) 
        self.topbar.destroy() 
        self.pluginlist.destroy()
        self.background.destroy()       
        
class PluginList(object):
    def __init__(self, parent, plugins=['music', 'pictures', 'movies']):
        self.parent = parent
        self.selected = 0
        self.mouse_active = mousehandler.active()
        eventhandler.subscribe(self)
        self.plugins = plugins
        self.plugin_xcoords = []
        self.get_positions()
        self.draw()
        if config.input == "keyboard + mouse" and not self.mouse_active: 
            self.hl_selected()
            
    def get_positions(self):
        self.number_of_plugins = len(self.plugins)            
        if self.number_of_plugins % 2 == 0: # even number of modules
            k = 1 # will enumerate beggining in 1, not 0 as in list
            for plugin in self.plugins:
                if k <= (self.number_of_plugins / 2):
                    self.plugin_xcoords.append(5 - ((1 + (self.number_of_plugins / 2) - k) * 10))
                else:
                    self.plugin_xcoords.append(-5 + ((k - self.number_of_plugins / 2) * 10))
                k+=1
        elif self.number_of_plugins % 2 == 1: # odd number of modules
            middle_plugin = abs(self.number_of_plugins / 2) + 1
            k = 1 # will enumerate beggining in 1, not 0 as in list
            for plugin in self.plugins:
                if k < middle_plugin:
                    self.plugin_xcoords.append(-(10 * (middle_plugin - k)))
                else:
                    self.plugin_xcoords.append(10 * (k - middle_plugin))
                k += 1
    def draw(self):
        self._w_plugins = {} 
        self._w_plugins_reflex = {} 
        self.color_plugins = qgl.scene.state.Color((1.0,1.0,1.0,0.0))
        self.color_plugins_reflex = qgl.scene.state.Color((1.0,1.0,1.0,0.0))
        k = 0
        for plugin in self.plugins:
            self._w_plugins[plugin] = qgl.scene.Group()
            img = os.path.join(config_data.theme, plugin + '.png')
            tex = qgl.scene.state.Texture(img)
            quad = qgl.scene.state.Quad((8,8))
            self._w_plugins[plugin].add(tex, self.color_plugins, quad)
            self._w_plugins[plugin].translate = (self.plugin_xcoords[k],1.0,-50)
            self._w_plugins[plugin].selectable = True
            viewport.add(self._w_plugins[plugin]) 
            k+=1
        k = 0
        for plugin in self.plugins:
            self._w_plugins_reflex[plugin] = qgl.scene.Group()
            img = os.path.join(config_data.theme, plugin + '.png')
            img = visuals.reflex(img, intensity=50.0)
            tex = qgl.scene.state.Texture(img)
            quad = qgl.scene.state.Quad((8,8))
            self._w_plugins_reflex[plugin].add(tex, self.color_plugins_reflex, quad)
            self._w_plugins_reflex[plugin].translate = (self.plugin_xcoords[k],-7.0,-50)
            self._w_plugins_reflex[plugin].selectable = True
            viewport.add(self._w_plugins_reflex[plugin]) 
            k+=1
        self._y_fade = actions.Fade(self.color_plugins, alpha=1.0, time=3.0)
        self._y_fade = actions.Fade(self.color_plugins_reflex, alpha=.5, time=3.0)
        
    def hl_plugin(self, selection):
        actions.Scale(self._w_plugins[self.plugins[self.selected]], scale=(1.0, 1.0, 1.0), time=.05)
        actions.Scale(self._w_plugins_reflex[self.plugins[self.selected]], scale=(1.0, 1.0, 1.0), time=.05)
        actions.Move(self._w_plugins_reflex[self.plugins[self.selected]], (self.plugin_xcoords[self.selected],-7.0,-50), time=.05)
        self.selected = selection
        actions.Scale(self._w_plugins[self.plugins[self.selected]], scale=(1.5, 1.5, 1.5), time=.05)
        actions.Scale(self._w_plugins_reflex[self.plugins[self.selected]], scale=(1.5, 1.5, 1.5), time=.05)
        actions.Move(self._w_plugins_reflex[self.plugins[self.selected]], (self.plugin_xcoords[self.selected],-11.0,-50), time=.05)
    
    def hl_selected(self):
        actions.Scale(self._w_plugins[self.plugins[self.selected]], scale=(1.5, 1.5, 1.5), time=.05)
        actions.Scale(self._w_plugins_reflex[self.plugins[self.selected]], scale=(1.5, 1.5, 1.5), time=.05)
        actions.Move(self._w_plugins_reflex[self.plugins[self.selected]], (self.plugin_xcoords[self.selected],-11.0,-50), time=.05)
                
    def unhl_selected(self):
        actions.Scale(self._w_plugins[self.plugins[self.selected]], scale=(1.0, 1.0, 1.0), time=.05)
        actions.Scale(self._w_plugins_reflex[self.plugins[self.selected]], scale=(1.0, 1.0, 1.0), time=.05)
        actions.Move(self._w_plugins_reflex[self.plugins[self.selected]], (self.plugin_xcoords[self.selected],-7.0,-50), time=.05)        
                                    
    def handle_events(self, events):
        if config.input == 'keyboard + mouse':
            for event in events:
                if event.type is KEYDOWN:
                    if event.key == K_RETURN:
                        self.parent.destroy()
                        import plugins.music.menu as menu
                        menu.MusicMenu()
                        root_node.accept(compiler)   
                    elif event.key == K_LEFT and not self.mouse_active and self.selected > 0 and self.number_of_plugins > 1:
                        self.hl_plugin(self.selected - 1)
                    elif event.key == K_RIGHT and not self.mouse_active and self.selected < (self.number_of_plugins-1) and self.number_of_plugins > 1:
                        self.hl_plugin(self.selected + 1)
                elif event.type == 'mouse_active':
                    self.mouse_active = True
                    pygame.mouse.set_visible(True)
                    self.unhl_selected()             
                elif event.type == 'mouse_inactive':
                    self.mouse_active = False
                    pygame.mouse.set_visible(False)
                    if not self._w_plugins[self.plugins[self.selected]].scale[0] > 1.0:
                        self.hl_selected()   
                        
        elif config.input == 'touchscreen':
            for event in events:
                if event.type is MOUSEBUTTONUP:
                    picker.set_position(event.pos)
                    root_node.accept(compiler)
                    root_node.accept(picker)                
                    if self._w_plugins['music'] in picker.hits:
                        self.parent.destroy()
                        import plugins.music.menu as menu
                        menu.MusicMenu()
                        root_node.accept(compiler)   
                                          
        elif config.input == 'remote':
            pass                       

    def destroy(self):
        eventhandler.unsubscribe(self)
        for plugin in self._w_plugins:
            viewport.remove(self._w_plugins[plugin])
            viewport.remove(self._w_plugins_reflex[plugin])
            